<template>
	<view class="bg-white radius shadow-blur margin">
		<view class="cu-item flex" style="flex-direction: column;">
			<view v-if="comment.post" @tap="postDetailAction"
				class="action flex justify-between align-center padding-sm solid-bottom" style="width: 100%;">
				<text v-if="comment.post.circle_id==0" class="text-bold text-black">
					《 {{comment.post.title}} 》
				</text>
				<text v-else class="text-bold text-black">
					评论动态
				</text>
				<view class="flex justify-end align-center" style="min-width: 140upx;">
					<text v-if="comment.post.circle_id==0"
						class="text-grey text-sm">{{comment.post.join_user_count}}人也评论了<text
							class="cuIcon-right text-gray"></text></text>
					<text v-else class="text-grey text-sm">查看详情<text class="cuIcon-right text-grey"></text></text>
				</view>
			</view>
			<view v-else class="padding solid-bottom" style="width: 100%;">
				<text class="text-bold text-grey">
					原帖已被作者删除
				</text>
			</view>
			<view class="flex align-start justify-start" style="width: 100%;">
				<view class="padding flex align-center justify-start" style="flex-direction: column;">
					<view @tap="jumpToUserProfile(comment.author.user_id)" class="cu-avatar round"
						:style="'background-image:url('+comment.author.avatar+')'">
					</view>
					<view v-if="comment.author.role!==null" class="padding-top-xs">
						<view style="max-width: 65upx;" :class="'radius cu-tag sm bg-'+roleColor">
							{{comment.author.role.name}}</view>
					</view>
				</view>
				<view class="flex justify-start align-start padding-right padding-top"
					style="flex-direction: column;width: 100%;">
					<view class="flex justify-between align-end padding-bottom-sm" style="width: 100%;">
						<view class="flex justify-start align-start" style="flex-direction: column;">
							<view class="flex justify-start align-center">
								<text @tap="jumpToUserProfile(comment.author.user_id)"
									class="text-grey text-sm">{{comment.author.nickname}}</text>
								<view v-if="comment.author.group!==null"
									class="flex justify-start align-center padding-left-xs">
									<view class="radius cu-tag sm bg-gray text-grey">{{comment.author.group.name}}
									</view>
								</view>
							</view>
							<text class="text-gray text-sm">{{comment.show_time}}</text>
						</view>
						<view class="flex justify-end align-center">
							<view v-if="enableDelete" class="action" @tap="deleteAction">
								<text class="cuIcon-delete text-gray"></text>
							</view>
							<view v-else-if="isManager && comment.audit_status !== -1" class="action"
								@tap="lockCommentAction(comment)">
								<text class="cuIcon-lock text-gray"></text>
							</view>
							<view v-else class="action" @tap="reportCommentAction(comment)">
								<text class="cuIcon-warn text-gray"></text>
							</view>
						</view>
					</view>
					<view @click="detailParentAction" v-if="comment.parent_comment" class="bg-gray padding-sm"
						style="width: 100%;">
						<view v-if="comment.parent_comment.content&&comment.parent_comment.content.length>0">
							<rich-text @longpress.stop="copyContent(comment.parent_comment.content)"
								@tap.stop="jumpToCommentDetail(comment.parent_comment)" class="reply-text"
								:nodes="combineContent(comment.parent_comment)"></rich-text>
						</view>
						<view v-if="comment.parent_comment.image_list && comment.parent_comment.image_list.length>0"
							style="width: 100%;">
							<view :style="'max-width:'+(comment.parent_comment.image_list.length>2?'80%;':'25%;')"
								:class="'grid flex-sub grid-square col-'+comment.parent_comment.image_list.length">
								<view class="bg-img" v-for="(url,index) in comment.parent_comment.image_list"
									:key="index" @click.stop="ViewImage" :data-list="comment.parent_comment.image_list"
									:data-url="url">
									<image :src="url+'?imageView2/2/w/500/h/500/q/75'" mode="aspectFill"></image>
								</view>
							</view>
						</view>
					</view>
					<view v-else-if="checkParentDelete" style="width: 100%;">
						<view v-if="comment.parent_comment_id" class="bg-gray padding-sm">
							<text class="text-grey" style="letter-spacing: 1.5upx;">原评论已被作者删除</text>
						</view>
					</view>
					<view v-if="comment.audit_status==-1"
						:class="(comment.parent_comment!==null||(comment.parent_comment_id&&checkParentDelete))?'padding-top-xs':''">
						<text class="text-grey" style="letter-spacing: 1.5upx;">内容涉嫌违规已被击飞</text>
					</view>
					<view v-else-if="comment.content && comment.content.length > 0"
						:class="(comment.parent_comment!==null||(comment.parent_comment_id&&checkParentDelete))?'padding-top-xs':''">
						<rich-text @longpress.stop="copyContent(comment.content)" @tap.stop="jumpToCommentDetail(comment)"
							class="text-df comment-text" :nodes="combineContent(comment)"></rich-text>
					</view>
					<view v-if="comment.audit_status!=-1 && comment.image_list && comment.image_list.length>0"
						style="width: 100%;"
						:class="'padding-top-'+[comment.content&&comment.content.length>0?'xs':'sm']">
						<view :style="'max-width:'+(comment.image_list.length>2?'80%;':'50%;')"
							:class="'grid flex-sub grid-square col-'+comment.image_list.length">
							<view class="bg-img" v-for="(url,index) in comment.image_list" :key="index" @tap="ViewImage"
								:data-list="comment.image_list" :data-url="url">
								<image :src="url+'?imageView2/2/w/500/h/500/q/75'" mode="aspectFill"></image>
							</view>
						</view>
					</view>
					<!-- @提到某人 -->
					<view v-if="comment&&comment.at_user_list&&comment.at_user_list.length>0"
						class="flex justify-start padding-top-sm" style="flex-wrap: wrap;">
						<view v-for="(item,index) in  comment.at_user_list" :key="index">
							<view class="padding-left-sm padding-top-sm">
								<view @tap="jumpToUserProfile(item.author.user_id)" class="round cu-tag text-blue"
									style="background-color: #eff6fb;">
									@{{item.author.nickname}}
								</view>
							</view>
						</view>
					</view>
					<view class="flex justify-between align-center padding-bottom-sm" style="width: 100%;">
						<view class="flex justify-start align-center" style="width: 80%;">
							<view v-if="comment.audit_status !== -1" class="padding-left-xs padding-top-sm">
								<view @tap="praiseAction(comment,index,false)"
									class="flex justify-end align-center padding-right-xl action">
									<text :class="'cuIcon-appreciate text-'+[comment.is_praise?'red':'gray']"></text>
									<text :class="'text-'+[comment.is_praise?'red':'gray']">点赞({{comment.praise_count}})</text>
								</view>
							</view>
							<view v-if="comment.reply_count>0" class="padding-top-sm">
								<view class="flex justify-end align-center action" @tap="detailAction">
									<text class="cuIcon-comment text-gray"></text>
									<text class="text-gray">评论 ({{comment.reply_count}})</text>
								</view>
							</view>
						</view>
						<view v-if="comment.audit_status !== -1" class="padding-left-xs padding-top-sm">
							<view class="flex justify-end align-center action" @tap="replyCommentAction(comment)">
								<text class="cuIcon-community text-gray"></text>
								<text class="text-gray">回复</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getTagColorForRole
	} from '@/api/utils.js'
	import {
		checkLogin
	} from '@/js_sdk/checkLogin.js'
	import {
		getReportTipReadStatus,
		setReportTipFinishRead,
		isLogin,
		getUserInfo
	} from '@/api/auth.js'
	export default {
		name: "UserCommentItem",
		data() {
			return {
				reg: /\#[\S]{1,3}\;/gi
			};
		},
		computed: {
			isManager() {
				if (isLogin() == false) {
					return false
				}
				let userInfo = getUserInfo()
				if (userInfo.role == null) {
					return false
				}
				return userInfo.role_id > 0
			},
			roleColor() {
				if (this.comment == null ||
					this.comment.author == null ||
					this.comment.author.role == null) {
					return 'white'
				}
				return getTagColorForRole(this.comment.author.role.role_id)
			}
		},
		props: {
			comment: {
				type: Object,
				default: {}
			},
			enableDelete: {
				type: Boolean,
				default: false
			},
			checkParentDelete: {
				type: Boolean,
				default: true
			}
		},
		methods: {
			copyContent(content) {
				uni.setClipboardData({
					data: content,
					success: function() {
						uni.showToast({
							icon: 'none',
							title: '内容已复制!'
						});
					}
				});
			},
			combineContent(comment, subLength) {
				let content = ""
				let commentContent = subLength && subLength.length > 0 ? comment.content.slice(0, subLength) + '...' :
					comment.content
				content += comment.audit_status == -1 ? '内容涉嫌违规已被击飞' : commentContent.replace(this.reg, this.emotion)
				let imageText = comment.image_list && comment.image_list.length > 0 ? '[图片]' : ''
				return content + imageText
			},
			replyEmotion(res) {
				//console.log('mess', res)
				let word = res.replace(/\#|\;/gi, '')
				//console.log(word)
				const list = ['微笑', '撇嘴', '色', '发呆', '得意', '流泪', '害羞', '闭嘴', '睡', '大哭', '尴尬', '发怒', '调皮', '呲牙', '惊讶', '难过',
					'酷',
					'冷汗', '抓狂', '吐', '偷笑', '可爱', '白眼', '傲慢', '饥饿', '困', '惊恐', '流汗', '憨笑', '大兵', '奋斗', '咒骂', '疑问', '嘘',
					'晕', '折磨', '衰',
					'骷髅', '敲打', '再见', '擦汗', '抠鼻', '鼓掌', '糗大了', '坏笑', '左哼哼', '右哼哼', '哈欠', '鄙视', '委屈', '快哭了', '阴险', '亲亲',
					'吓', '可怜',
					'菜刀', '西瓜', '啤酒', '篮球', '乒乓', '咖啡', '饭', '猪头', '玫瑰', '凋谢', '示爱', '爱心', '心碎', '蛋糕', '闪电', '炸弹', '刀',
					'足球', '瓢虫',
					'便便', '月亮', '太阳', '礼物', '拥抱', '强', '弱', '握手', '胜利', '抱拳', '勾引', '拳头', '差劲', '爱你', 'NO', 'OK', '爱情',
					'飞吻', '跳跳',
					'发抖', '怄火', '转圈', '磕头', '回头', '跳绳', '挥手', '激动', '街舞', '献吻', '左太极', '右太极'
				]
				let index = list.indexOf(word)
				return `<img src="https://res.wx.qq.com/mpres/htmledition/images/icon/emotion/${index}.gif" style="vertical-align:middle;" width="14" height="14">`
			},
			emotion(res) {
				//console.log('mess', res)
				let word = res.replace(/\#|\;/gi, '')
				//console.log(word)
				const list = ['微笑', '撇嘴', '色', '发呆', '得意', '流泪', '害羞', '闭嘴', '睡', '大哭', '尴尬', '发怒', '调皮', '呲牙', '惊讶', '难过',
					'酷',
					'冷汗', '抓狂', '吐', '偷笑', '可爱', '白眼', '傲慢', '饥饿', '困', '惊恐', '流汗', '憨笑', '大兵', '奋斗', '咒骂', '疑问', '嘘',
					'晕', '折磨', '衰',
					'骷髅', '敲打', '再见', '擦汗', '抠鼻', '鼓掌', '糗大了', '坏笑', '左哼哼', '右哼哼', '哈欠', '鄙视', '委屈', '快哭了', '阴险', '亲亲',
					'吓', '可怜',
					'菜刀', '西瓜', '啤酒', '篮球', '乒乓', '咖啡', '饭', '猪头', '玫瑰', '凋谢', '示爱', '爱心', '心碎', '蛋糕', '闪电', '炸弹', '刀',
					'足球', '瓢虫',
					'便便', '月亮', '太阳', '礼物', '拥抱', '强', '弱', '握手', '胜利', '抱拳', '勾引', '拳头', '差劲', '爱你', 'NO', 'OK', '爱情',
					'飞吻', '跳跳',
					'发抖', '怄火', '转圈', '磕头', '回头', '跳绳', '挥手', '激动', '街舞', '献吻', '左太极', '右太极'
				]
				let index = list.indexOf(word)
				return `<img src="https://res.wx.qq.com/mpres/htmledition/images/icon/emotion/${index}.gif" style="vertical-align:middle;" width="20" height="20">`
			},
			lockCommentAction(comment) {
				this.$emit('lock')
			},
			jumpToUserProfile(userId) {
				uni.navigateTo({
					url: '/pages/profile/profile?userId=' + userId
				})
			},
			deleteAction() {
				this.$emit('delete')
			},
			postDetailAction() {
				if (this.comment.post.circle_id > 0) {
					uni.navigateTo({
						url: '/pages/lessuse/active-list/active-list?postId=' + this.comment.post_id
					})
				} else {
					uni.navigateTo({
						url: '/pages/detail/detail?postId=' + this.comment.post_id
					})
				}

			},
			replyCommentAction() {
				this.$emit('reply')
			},
			reportCommentAction() {
				if (isLogin() == false) {
					checkLogin()
					return
				}
				let status = getReportTipReadStatus()
				if (status == false) {
					let that = this
					uni.showModal({
						title: '平台提示',
						content: '请如实根据当前需举报内容的真实性进行举报，如果多次举报被判定为无效举报或者恶意举报将会被平台永久拉黑!',
						confirmText: '知道了',
						cancelText: '手滑了',
						success(res) {
							if (res.confirm) {
								that.$emit('report')
							}
						},
						complete() {
							setReportTipFinishRead()
						}
					})

				} else {
					this.$emit('report')
				}
			},
			praiseAction() {
				this.$emit('praise')
			},
			detailAction() {
				this.$emit('detail')
			},
			detailParentAction() {
				this.$emit('detailParent')
			},
			ViewImage(e) {
				console.log(e.currentTarget.dataset.list)
				console.log(e.currentTarget.dataset.url)
				uni.previewImage({
					urls: e.currentTarget.dataset.list,
					current: e.currentTarget.dataset.url
				});
			},
		},
	}
</script>

<style lang="scss">
	.comment-text {
		font-size: 30upx;
		color: #333333;
		letter-spacing: 1.5upx;
	}

	.reply-text {
		color: #888;
		letter-spacing: 1.5upx;
	}
</style>
